package com.ubs.training.server.dao.impl.hbm;

import com.ubs.training.server.dao.ClientDao;
import com.ubs.training.server.dao.model.Client;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("hbmClientDao")
public class HbmClientDao implements ClientDao {


    private SessionFactory factory;

    private HibernateTemplate tpl;

    public SessionFactory getFactory() {
        return factory;
    }

    @Autowired
    public void setFactory(SessionFactory factory) {
        tpl = new HibernateTemplate(factory);
        this.factory = factory;
    }

    @Override
    public List<Client> getAll() {
        return tpl.loadAll(Client.class);
    }

    @Override
    public Client getById(int id) {
        return tpl.get(Client.class, id);
    }

    @Override
    public boolean add(Client client) {
        tpl.save(client);
        return true;
    }

    @Override
    public boolean update(Client client) {
        tpl.merge(client);
        tpl.update(client);
        return true;
    }

    @Override
    public boolean remove(Client client) {
        tpl.merge(client);
        tpl.delete(client);
        return true;
    }

    @Override
    public void remove(int clientId) {
        // todo: implement
    }
}
